package com.wjb.Job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author : 王济彬
 * @date: 2025/6/10 - 06 - 10 - 15:54
 * @Description: com.wjb.Job
 * @version: 1.0
 */
@Component
@Slf4j
public class OrderETLJob implements Job {

    @Autowired
    private JdbcTemplate  jdbcTemplate;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("开始执行订单ETL任务");

        //抽取：查询订单表和产品表
        String orderSql = "select o.order_id,o.order_name,o.quantity,p1.product_name,p1.product_price,p2.address,p2.specification " +
                "from orderx o " +
                "Left join product p1 On o.product_id = p1.product_id " +
                "Left join productdescribe p2 On p1.product_id = p2.product_id";
        List<Map<String,Object>> orderList = jdbcTemplate.queryForList(orderSql);

        //转换：流式处理

        //加载：插入到数据仓库
        String insertSql = "REPLACE INTO dw_order_summary (order_id, quantity, product_name, order_name, address, specification) " +
                "VALUES (?, ?, ?, ?, ?, ?)";
        for(Map<String,Object> row : orderList){
            jdbcTemplate.update(insertSql,
                    row.get("order_id"),
                    row.get("quantity"),
                    row.get("product_name"),
                    row.get("order_name"),
                    row.get("address"),
                    row.get("specification")
            );
        }
        log.info("订单ETL任务执行完毕，处理记录数：{}",orderList.size());



    }
}
